import jquery from 'jquery'
import { loadScript } from './jsload';
loadScript('http://qiniu.liumingtai.com/qiniu.min.js')
import { getTimeForQiniu } from "./date"
export function getToken () {
    var baseurl = "http://www.liumingtai.com:7778/qiniu/token"
    //  var baseurl="http://127.0.0.1:7778/qiniu/token"
    return jquery.post(baseurl, {
        username: "liumingtai",
        password: "liumingtai"
    }).then(res => {
        return res.token
    })
}
export async function uploadImage (file) {
    if (!file) return false
    var token = await getToken()
    var key = getTimeForQiniu() + (file.name || "a_" + (new Date()).getSeconds())
    var config = {
        // useCdnDomain: true,
        region: window.qiniu.region.z2,
        uptoken: token,
        domain: 'http://qiniu.liumingtai.com/',
        unique_names: true,
    };
    var putExtra = {
        // fname: file.name,
        // params: {},
        // mimeType: [] || null
        ...config,
    }
    var observable = window.qiniu.upload(file, key, token, putExtra, config)
    return new Promise((resolve, reject) => {
        observable.subscribe({
            error (err) {
                reject(err)
            },
            complete (data) {
                data.path = `http://qiniu.liumingtai.com/${data.key}`
                resolve(data)
            }
        }) // 上传开始
    })
}
